// Do not remove the include below
#include "NesUsbUno.h"
#include "NESState.h"
#include "gamepadreport.h"


const int CLOCK_PIN = A0;
const int STROBE_PIN = A1;
const int DATA_PIN = A2;

static NESState g_nesState(DATA_PIN, STROBE_PIN, CLOCK_PIN);


//The setup function is called once at startup of the sketch
void setup()
{
	 Serial.begin(115200);
	 delay(200);
}

#define FIRST_BIT		(1<<0)
#define SECOND_BIT		(1<<1)
#define THIRD_BIT		(1<<2)
#define FOURTH_BIT		(1<<3)

// The loop function is called in an endless loop
void loop()
{
	// check if nes buttons were pushed
	if (g_nesState.refresh())
	{
		USB_GamepadReport_Data_t gamepadReport = {0};

#ifndef _GAMEPADREPORT
		if (g_nesState.buttonIsPressed(NESState::A))		gamepadReport.buttons |= FIRST_BIT;
		if (g_nesState.buttonIsPressed(NESState::B))		gamepadReport.buttons |= SECOND_BIT;
		if (g_nesState.buttonIsPressed(NESState::SELECT))	gamepadReport.buttons |= THIRD_BIT;
		if (g_nesState.buttonIsPressed(NESState::START))	gamepadReport.buttons |= FOURTH_BIT;

		if (g_nesState.buttonIsPressed(NESState::UP))		gamepadReport.y = 0;
		if (g_nesState.buttonIsPressed(NESState::DOWN))		gamepadReport.y = 0;

		if (g_nesState.buttonIsPressed(NESState::LEFT))		gamepadReport.x = 0;
		if (g_nesState.buttonIsPressed(NESState::RIGHT))	gamepadReport.x = 0;

#else
		g_nesState.createGamepadReport(gamepadReport);

#endif /* NESSTATE_H_ */

		// write the report out to serial as byte stream
		Serial.write((uint8_t*) &gamepadReport, sizeof(USB_GamepadReport_Data_t));
		delay(1000);
	}
}
